function tmean = fsb_meancalc(idat,dim)

% FSB : Function to calculate the mean of a 4-D matrix in any dimension
%
% EXAMPLE:
% tmean = fsb_meancalc(idat,4)
%
% INPUT:
% idat:         4-D image data
% dim:          dimension over which mean is to be calculated
%
% OUTPUT:
% tmean:        mean of 4-D matrix
%
% CALLED BY:
% fsb_remove_disc.m
% fsb_detrend_scan.m
% fsb_ROI_mask.m
% fsb_det_artifact_mod.m
%
% NOTES:
% This function was written to simplify fMRI Sandbox code
%
% Copyright 2010 MPI for Biological Cybernetics
% Author: Steffen Stoewer
% License: GNU GPL, no express or implied warranties
% 
% $Revision 1.0
%
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

idat3 = shiftdim(idat,dim-1);
idat3 = reshape(idat3,size(idat3,1),[]);
tmean = nanmean(idat3,2);

end
